﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using webapp4U;
using webapp4U.BOL;

public partial class UserControl_ControlInCacNghiepVuNhanHang : System.Web.UI.UserControl
{
    ClassExecute.ClassExecute dt;
    string str = webapp4U.Configuration.CurrentConnectionString;
    string error = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {  
            DataBind();
        }
        if (ViewState["strSoHoaDon"] != null)
            ControlReportViewer1.ReportViewer.Report = CreateReport(ViewState["strSoHoaDon"].ToString());
    }

    public override void DataBind()
    {
        if (Session["Sitecode"] != null)
        {
            dt = new ClassExecute.ClassExecute();
            DataTable db = dt.Get_Data_DataTable(str, ref error, "ls_getNotPrintedVoucherList", CommandType.StoredProcedure, new SqlParameter("@SiteCode", Session["Sitecode"].ToString()));
            if (db.Rows.Count > 0)
            {
                grdvNghiepVuNhanHangLists.DataSource = db;
                grdvNghiepVuNhanHangLists.DataBind();
            }
        }
    }

    //protected void ImgFind_Click(object sender, ImageClickEventArgs e)
    //{
    //    if (Session["Sitecode"] != null)
    //    {
    //        dt = new ClassExecute.ClassExecute();
    //        DataTable db = dt.Get_Data_DataTable(str, ref error, "TTr_getNotPrintedVoucherListFilter", CommandType.StoredProcedure, new SqlParameter("@SiteCode", Session["Sitecode"].ToString()),
    //            new SqlParameter("@Filter", txtFilter.Text));
    //        if (db.Rows.Count > 0)
    //        {
    //            grdvNghiepVuNhanHangLists.SelectedIndex = -1;
    //            grdvNghiepVuNhanHangLists.DataSource = db;
    //            grdvNghiepVuNhanHangLists.DataBind();
    //        }
    //    }
    //}
    protected void grdvNghiepVuNhanHangLists_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (Session["Sitecode"] != null)
        {
            GridViewRow row = grdvNghiepVuNhanHangLists.Rows[grdvNghiepVuNhanHangLists.SelectedIndex];
            Session["NgayHoaDonNhanHang"] = ((Label)row.FindControl("lbNgayHoaDon")).Text;
            string strSoHoaDon = grdvNghiepVuNhanHangLists.SelectedDataKey.Value.ToString();
            dt = new ClassExecute.ClassExecute();
            //lbNgayHoaDon
            dt.ExecuteSQl_bool(str, CommandType.StoredProcedure, "TTr_UpdateVoucher4Printed", ref error,
                new SqlParameter("@vouchno", strSoHoaDon), new SqlParameter("@SiteCode", Session["Sitecode"].ToString()));
            //ReportViewer1.Report = CreateReport(strSoHoaDon);
            ViewState["strSoHoaDon"] = strSoHoaDon;
            ControlReportViewer1.ReportViewer.Report = CreateReport(strSoHoaDon);
            TabContainer1.ActiveTabIndex = 1;
            DataBind();

            //UpdatePanel1.Update();
            
            //string url = "~/Pages/InCacNghiepVuNhanHang/Printing.aspx?VchNo=" + strSoHoaDon;
            //webapp4U.ResponseHelper.Redirect(this.Response, url, "_blank", "menubar=0,width=900,height=700");
            
            //Response.Redirect("~/Pages/InCacNghiepVuNhanHang/Printing.aspx?VchNo=" + strSoHoaDon);
            
        }
    }

    DevExpress.XtraReports.UI.XtraReport CreateReport(string strSoHoaDon)
    {
        XtraReportInNhanHangTuNCC tableReport = new XtraReportInNhanHangTuNCC();
        if (Session["Sitecode"] != null)
        {
            string VchNo = strSoHoaDon;
            //if (Request.QueryString["VchNo"] != null)
            //{
            //    VchNo = Request.QueryString["VchNo"].ToString();
            //}
            dt = new ClassExecute.ClassExecute();
            DataSet ds = new DataSet();

            ds = dt.Get_Data_DataSet(str, ref error, "ls_getVoucher4Print", CommandType.StoredProcedure,
                new SqlParameter("@VchNo", VchNo),
                new SqlParameter("@siteCode", Session["Sitecode"].ToString()));

            string SiteName = "";
            DataTable data = dt.Get_Data_DataTable(str, ref error, "ls_getStoreName", CommandType.StoredProcedure, new SqlParameter("@siteCode", Session["Sitecode"].ToString()));
            if (data.Rows.Count > 0)
            {
                SiteName = data.Rows[0]["sitename"].ToString();
            }
            string strNgayHoaDon = "";
            if (Session["NgayHoaDonNhanHang"] != null)
                strNgayHoaDon = Session["NgayHoaDonNhanHang"].ToString();
            tableReport.SetReportParameter(ds, SiteName, VchNo, strNgayHoaDon);
        }
        return tableReport;
    }
    protected void grdvNghiepVuNhanHangLists_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grdvNghiepVuNhanHangLists.SelectedIndex = -1;
        grdvNghiepVuNhanHangLists.PageIndex = e.NewPageIndex;
        DataBind();
    }
   
    protected void imgBtnAdd_Click(object sender, ImageClickEventArgs e)
    {
        DataBind();
    }
}